layui.define(['jquery','config','layer'],function(exports) {
    var $ = layui.jquery;
    var $layer = layui.layer;
    var $config = layui.config();

    var FRAME = function(){};
    FRAME.prototype.openWindow = function (opts) {
        var url = $config.config.baseUrl + opts.url;
        
        if(opts.data){
            var subUrl = '?';
            $.each(opts.data,function (key,value) {
                subUrl += key + "=" + value + "&";
            });
            url += subUrl.substring(0,subUrl.length-1);
        }

        var win = $(window);
        var isSubmit = true;
        var idx = $layer.open({
            title: opts.title,
            type: 2,
            anim: 5,
            scrollbar: false,
            area:[win.width(),win.height()],
            // offset :[win.scrollTop(),win.scrollLeft()],
            offset: ['100px', '50px'],
            content: url,
            success: function (layero, index) {
                $layer.full(index);
                setTimeout(function () {
                    $layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },300);
            },
            cancel: function (index, layero) {
                isSubmit = false;
            },
            end: function (index, layero) {
                if(isSubmit && typeof(opts.callback) == "function"){
                    opts.callback(index, layero);
                }
            }
        });

        return idx;
    };

    FRAME.prototype.closeWindow = function(idx){
        if(idx){
            $layer.close(idx);
        }else{
            $layer.closeAll(idx);
        }
    };

    FRAME.prototype.messageBox = function(msg,callback){
        var _this = this;
        $layer.open({
            title:'温馨提示'
            ,type: 1
            ,offset: 'auto'
            ,id: 'layerMessageBox' //防止重复弹出
            ,content: '<div style="padding: 20px 100px;">'+ msg +'</div>'
            ,btn: ['确定','取消']
            ,btnAlign: 'c' //按钮居中
            ,shade: 0.5 //不显示遮罩
            ,btn1 : function (index, layero) {
                if(typeof(callback) == "function"){
                    callback(index, layero);
                }else{
                    $layer.msg("请求数据出错！");
                }
            }
            ,btn2 : function(index, layero) {
                _this.closeWindow(index);
            }
        });
    };

    //浏览器DEBUG模式下 拉动窗口内联frame自动更改大小
    $(window).bind("resize",function () {
        $.each($(".layui-layer-iframe"),function () {
            $layer.full($(this).attr("times"));
        });
    });

    //提示框
    FRAME.prototype.message = function(msg){
        $layer.msg(msg);
    };

    var frame = new FRAME();
    exports("frame",function(option){
        return frame;
    });
    
    //关闭iframe层 （当前层）
    FRAME.prototype.close = function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    };
});
